package org.jvnc.test;

/**
* <br><br><center><table border="1" width="80%"><hr>
* <p>
* Copyright (C) 2006-2007 by Deon Wu
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.0
* of the License, or (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* <a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html">GNU Lesser General Public License</a>
* for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html">
* GNU Lesser General Public License</a> along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* <hr></table></center>
* **/

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyboardFocusManager;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jvnc.vnc.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Collection;

import javax.swing.WindowConstants;

public class ClipDemo extends javax.swing.JFrame implements KeyListener{
	private JPanel panelMenu;
	private JButton btnConnect;
	private JLabel labIP;
	private JButton btnRefresh;
	private JLabel labPort;
	private JTextField txtPort;
	private JTextField txtIP;
	private JLabel labPwd;
	private JTextField txtPwd;
	private JButton reboot;
	
	private VncClient client = null;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		ClipDemo inst = new ClipDemo();		
		inst.setVisible(true);
	}
	
	public ClipDemo() {
		super();
		client = new VncClient();
		//client.getVncScreen().setClip(0, 0, 1024, 768);
		client.getVncScreen().setPreferredSize(new Dimension(1024,768));
		initGUI();
		//this.addKeyListener(this);
	}
	
	private void initGUI() {
		try {

			panelMenu = new JPanel();
			FlowLayout menuLayout = new FlowLayout();
			menuLayout.setAlignment(FlowLayout.LEFT);
			panelMenu.setLayout(menuLayout);
			getContentPane().add(panelMenu, BorderLayout.NORTH);

			labIP = new JLabel();
			panelMenu.add(labIP);
			labIP.setText("IP:");

			txtIP = new JTextField();
			panelMenu.add(txtIP);
			txtIP.setText("10.11.197.100");
			txtIP.setPreferredSize(new java.awt.Dimension(97, 20));

			labPort = new JLabel();
			panelMenu.add(labPort);
			labPort.setText("Port:");

			txtPort = new JTextField();
			panelMenu.add(txtPort);
			txtPort.setText("5900");
			txtPort.setPreferredSize(new java.awt.Dimension(43, 20));
			
			labPwd = new JLabel();
			panelMenu.add(labPwd);
			labPwd.setText("Password:");

			txtPwd = new JTextField();
			panelMenu.add(txtPwd);
			txtPwd.setText("123");
			txtPwd.setPreferredSize(new java.awt.Dimension(60, 20));

			btnConnect = new JButton();
			panelMenu.add(btnConnect);
			btnConnect.setText("Connect");
			btnConnect.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					client.close();
					client.connect(txtIP.getText(), Integer.parseInt(txtPort.getText()), txtPwd.getText());
				}
			});

			btnRefresh = new JButton();
			panelMenu.add(btnRefresh);
			btnRefresh.setText("Refresh");
			btnRefresh.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					client.refreshFrame();
				}
			});
			
			reboot = new JButton();
			panelMenu.add(reboot);
			reboot.setText("Ctrl+Alt+Delete");
			reboot.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					client.sendCtrlAltDelete();
				}
			});
			

			getContentPane().add(client.getVncScreen(),BorderLayout.CENTER);
			//getContentPane().add
//			Canvas c = new Canvas();
//			getContentPane().add(c);
//			c.addKeyListener(this);
//			ckm = new KFM(c);
//			c.addMouseListener(new ML());
//			btnConnect.setEnabled(false);
			
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					client.close();
					setVisible(false);
				}
			});
			
			pack();
			this.setSize(485, 331);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void keyPressed(KeyEvent arg0) {
		System.out.println("keyPressed:" + arg0.getKeyCode());
		arg0.consume();
	}

	public void keyReleased(KeyEvent arg0) {
		System.out.println("keyReleased:" + arg0.getKeyCode());
		arg0.consume();
	}

	public void keyTyped(KeyEvent arg0) {
		System.out.println("keyTyped:" + arg0.getKeyCode());
		arg0.consume();
	}
	
	KeyboardFocusManager ckm = null;	
	class ML implements MouseListener{

		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		KeyboardFocusManager km = null;
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("mouseEntered");
			km = KeyboardFocusManager.getCurrentKeyboardFocusManager();
			KeyboardFocusManager.setCurrentKeyboardFocusManager(ckm);
			//km.getd
			
		}

		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("mouseExited");
			if(km != null)
				KeyboardFocusManager.setCurrentKeyboardFocusManager(km);
			
		}

		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	class KFM extends java.awt.DefaultKeyboardFocusManager
	{
		Component c;
		KFM(Component c){
			super();
			this.c  = c;
			Collection<java.awt.KeyEventDispatcher> kds = getKeyEventDispatchers();
			if(kds != null)
			for(java.awt.KeyEventDispatcher kd : kds){
				this.removeKeyEventDispatcher(kd);
			} 
		}
		
		public boolean dispatchKeyEvent(KeyEvent e){
			//redispatchEvent(c, e);
			//redispatchEvent(Component target, AWTEvent e) 
			System.out.println("dispatchKeyEvent id:" + e.getID() +
					" code:" + e.getKeyCode()
					);
			e.consume();
			return false;
		} 
	}
}
